<?php

/**
 * Description of listUser
 *
 * @author MariiTeheran
 */

require_once '../conexion.php';

class listUser {
    
    private $conx;
    private $table;
    private $race = "SELECT * FROM RACE WHERE racecod <=6";
    private $food = "SELECT * FROM FOOD";
    private $bacth = "SELECT b.batchcod, b.batchname, b.landcod,l.landname, l.landcod 
                      FROM batch as b,land as l 
                      WHERE  l.userid='%s' and b.landcod = l.landcod and l.landname='%s'";
    private $cattle = "SELECT * FROM cattle WHERE batchcod = '%s'";
   /* private $foodcod = "SELECT b.foodbatch, f.namefood, b.batchcod, l.userid, l.landcod, b.landcod, b.batchname
                        FROM batch as b, food as f, land as l 
                        WHERE userid='%s' and b.landcod = l.landcod and batchname='%s'";
    private $racecod = "SELECT b.racecod, r.namerace, b.batchcod, l.userid, l.landcod, b.landcod, b.batchname
                        FROM batch as b, race as r, land as l 
                        WHERE userid='%s' and b.landcod = l.landcod and batchname='%s'"; */


    public function __construct() {
        $this->conx = null;
    }
    
    public function listRaces(){
        $consult = sprintf($this->race);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        if(!$result){
            echo 'Erroor listando las Razas \n';
        } while ($rows = pg_fetch_row($result)){
            echo "<option value={$rows[0]}>{$rows[1]}</option>";
        }
        return $this->table;
    }
    
    
    public function listFood(){
        $consult = sprintf($this->food);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        if(!$result){
            echo 'Erroor listando los Alimentos \n';
        } while ($rows = pg_fetch_row($result)){
            echo "<option value={$rows[0]}>{$rows[1]}</option>";
        }
        return $this->table;
    }
    
    public function listBatch($id,$landname){
        $consult = sprintf($this->bacth, $id, $landname);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        if(!$result){
            echo 'Erroor listando los Lotes \n';
        } while ($rows = pg_fetch_row($result)){
            echo "<option value={$rows[0]}>{$rows[1]}</option>";
        }
        return $this->table;
    }
    
    public function listCattle($batchid){
        $this->table = "";
        $consult = sprintf($this->cattle, $batchid);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        $row = pg_num_rows($result);
        if ($row == 0){
            $this->table .= "<td>No se Encuentran Ganados para este Lote</td>";
        } else {
            while ($rows = pg_fetch_array($result)){
                $this->table .= "<tr>";
                $this->table .= "<td>{$rows['cattlecod']}</td>";
                $this->table .= "<td>{$rows['birthdayca']}</td>";
                $this->table .= "<td>{$rows['weightbirth']}</td>";
                $this->table .= "<td>{$rows['weightweaning']}</td>";
                $this->table .= "<td>{$rows['weightact']}</td>";
                $this->table .= "<td>{$rows['ageact']}</td>";
                $this->table .= "<td>{$rows['stage']}</td>";
                $this->table .= "<td>{$rows['dateregister']}</td>";
                $this->table .= "</tr>";
            }
        }
        return $this->table;
    }


    /*public function listFoodCod($id,$batchname){
        $consult = sprintf($this->foodcod, $id, $batchname);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        if(!$result){
            echo 'Error listando el Alimento del Lote \n';
        } while ($rows = pg_fetch_row($result)){
            echo "<option value={$rows[0]}>{$rows[1]}</option>";
        }
        return $this->table;
    } */
    
   /* public function listRaceCod($id,$batchname){
        $consult = sprintf($this->racecod, $id, $batchname);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $consult);
        $this->conx->closeConexionDB();
        $this->conx = null;
        if(!$result){
            echo 'Error listando la Raza  del Lote \n';
        } while ($rows = pg_fetch_row($result)){
            echo "<option value={$rows[0]}>{$rows[1]}</option>";
        }
        return $this->table;
    } */
    
    
    
}

?>
